package com.hang.ad.sender.index;

import com.alibaba.fastjson.JSON;
import com.hang.ad.dump.table.*;
import com.hang.ad.handler.AdLevelDataHandler;
import com.hang.ad.index.DateLevel;
import com.hang.ad.mysql.constant.Constant;
import com.hang.ad.mysql.dto.MysqlRowData;
import com.hang.ad.sender.ISender;
import com.hang.ad.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Component("indexSender")
public class IndexSender implements ISender {
    @Override
    public void sender(MysqlRowData mysqlRowData) {
        String level = mysqlRowData.getLevel();
        if (DateLevel.LEVEL2.getLevel().equals(level)){
            level2RowData(mysqlRowData);
        }else if (DateLevel.LEVEL3.getLevel().equals(level)){
            level3RowData(mysqlRowData);
        }else if (DateLevel.LEVEL4.getLevel().equals(level)){
            level4RowData(mysqlRowData);
        }else {
            log.error("mysqlRowData error{}", JSON.toJSONString(mysqlRowData));
        }
    }

    private void level2RowData(MysqlRowData mysqlRowData){

        if (mysqlRowData.getTableName().equals(Constant.AD_PLAN_TABLE_INFO.TABLE_NAME)){
            List<AdPlanTable> planTables = new ArrayList<>();
            for (Map<String, String> map : mysqlRowData.getFieldValueMap()) {
                AdPlanTable table = new AdPlanTable();
                map.forEach((k,v)-> {
                    switch (k){
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_ID:
                            table.setId(Long.valueOf(v));
                            break;
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_USER_ID:
                            table.setUserId(Long.valueOf(v));
                            break;
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_PLAN_STATUS:
                            table.setPlanStatus(Integer.valueOf(v));
                            break;
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_START_DATE:
                            table.setStartDate(CommonUtils.parse2Date(v));
                            break;
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_END_DATE:
                            table.setEndDate(CommonUtils.parse2Date(v));
                            break;
                    }
                });
                planTables.add(table);
            }
            planTables.forEach(p -> AdLevelDataHandler.handleLevel2(p, mysqlRowData.getOpType()));
        }else if (mysqlRowData.getTableName().equals(Constant.AD_CREATIVE_TABLE_INFO.TABLE_NAME)){
            List<AdCreativeTable> creativeTables = new ArrayList<>();
            for (Map<String, String> map : mysqlRowData.getFieldValueMap()) {
                AdCreativeTable table = new AdCreativeTable();
                map.forEach((k,v)-> {
                    switch (k){
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_ID:
                            table.setAdId(Long.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_TYPE:
                            table.setType(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_MATERIAL_TYPE:
                            table.setMaterialType(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_HEIGHT:
                            table.setHeight(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_WIDTH:
                            table.setWidth(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_AUDIT_STATUS:
                            table.setAuditStatus(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_URL:
                            table.setAdUrl(v);
                            break;
                    }
                });
                creativeTables.add(table);
            }

            creativeTables.forEach(c -> AdLevelDataHandler.handleLevel2(c, mysqlRowData.getOpType()));
        }
    }

    private void level3RowData(MysqlRowData mysqlRowData){
        if (mysqlRowData.getTableName().equals(Constant.AD_UNIT_TABLE_INFO.TABLE_NAME)){
            List<AdUnitTable> unitTables = new ArrayList<>();

            for (Map<String, String> map : mysqlRowData.getFieldValueMap()) {
                AdUnitTable unitTable = new AdUnitTable();
                map.forEach((k,v) -> {
                    switch (k){
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_ID:
                            unitTable.setUnitId(Long.valueOf(v));
                            break;
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_UNIT_STATUS:
                            unitTable.setUnitStatus(Integer.valueOf(v));
                            break;
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_POSITION_TYPE:
                            unitTable.setPositionType(Integer.valueOf(v));
                            break;
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_PLAN_ID:
                            unitTable.setPlanId(Long.valueOf(v));
                            break;
                    }
                });
                unitTables.add(unitTable);
            }

            unitTables.forEach(u -> AdLevelDataHandler.handleLevel3(u, mysqlRowData.getOpType()));
        }else if (mysqlRowData.getTableName().equals(Constant.AD_CREATIVE_UNIT_TABLE_INFO.TABLE_NAME)){

            List<AdCreativeUnitTable> creativeUnitTables = new ArrayList<>();

            for (Map<String, String> map : mysqlRowData.getFieldValueMap()) {
                AdCreativeUnitTable adCreativeUnitTable = new AdCreativeUnitTable();
                map.forEach((k,v)->{
                    switch (k){
                        case Constant.AD_CREATIVE_UNIT_TABLE_INFO.COLUMN_CREATIVE_ID:
                            adCreativeUnitTable.setAdId(Long.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_UNIT_TABLE_INFO.COLUMN_UNIT_ID:
                            adCreativeUnitTable.setUnitId(Long.valueOf(v));
                            break;
                    }
                });
                creativeUnitTables.add(adCreativeUnitTable);
            }

            creativeUnitTables.forEach(u -> AdLevelDataHandler.handleLevel3(u, mysqlRowData.getOpType()));
        }
    }

    private void level4RowData(MysqlRowData mysqlRowData){
        switch (mysqlRowData.getTableName()){
            case Constant.AD_UNIT_DISTRICT_TABLE_INFO.TABLE_NAME:
                List<AdUnitDistrictTable> districtTables = new ArrayList<>();
                for (Map<String, String> map : mysqlRowData.getFieldValueMap()) {
                    AdUnitDistrictTable table = new AdUnitDistrictTable();

                    map.forEach((k,v)->{
                        switch (k){
                            case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_UNIT_ID:
                                table.setUnitId(Long.valueOf(v));
                                break;
                            case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_PROVINCE:
                                table.setProvince(v);
                                break;
                            case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_CITY:
                                table.setCity(v);
                                break;
                        }
                    });
                    districtTables.add(table);
                }
                districtTables.forEach(d -> AdLevelDataHandler.handleLevel4(d, mysqlRowData.getOpType()));
                break;
            case Constant.AD_UNIT_IT_TABLE_INFO.TABLE_NAME:
                List<AdUnitItTable> itTables = new ArrayList<>();
                for (Map<String, String> map : mysqlRowData.getFieldValueMap()) {
                    AdUnitItTable table = new AdUnitItTable();

                    map.forEach((k,v)->{
                        switch (k){
                            case Constant.AD_UNIT_IT_TABLE_INFO.COLUMN_UNIT_ID:
                                table.setUnitId(Long.valueOf(v));
                                break;
                            case Constant.AD_UNIT_IT_TABLE_INFO.COLUMN_IT_TAG:
                                table.setItTag(v);
                                break;
                        }
                    });
                    itTables.add(table);
                }
                itTables.forEach(i -> AdLevelDataHandler.handleLevel4(i, mysqlRowData.getOpType()));
                break;
            case Constant.AD_UNIT_KEYWORD_TABLE_INFO.TABLE_NAME:
                List<AdUnitKeywordTable> keywordTables = new ArrayList<>();
                for (Map<String, String> map : mysqlRowData.getFieldValueMap()) {
                    AdUnitKeywordTable table = new AdUnitKeywordTable();

                    map.forEach((k,v)->{
                        switch (k){
                            case Constant.AD_UNIT_KEYWORD_TABLE_INFO.COLUMN_UNIT_ID:
                                table.setUnitId(Long.valueOf(v));
                                break;
                            case Constant.AD_UNIT_KEYWORD_TABLE_INFO.COLUMN_KEYWORD:
                                table.setKeyword(v);
                                break;
                        }
                    });
                    keywordTables.add(table);
                }
                keywordTables.forEach(k-> AdLevelDataHandler.handleLevel4(k, mysqlRowData.getOpType()));
                break;
        }
    }


}
